<template>
  <div class="root gap-col-8 px-4 pt-2">
    <div v-if="!loading" class="font-size-14 font-weight-500 line-height-125">
      <i18n-t keypath="search.result.keywords" tag="div" class="font-size-14 font-weight-500 line-height-125">
        <template #keyword>
          <span class="text-primary">{{ searchText }}</span>
        </template>
        <template #num>
          <span>{{ dataExtra?.total }}</span>
        </template>
      </i18n-t>
    </div>
    <div class="gap-col-16" ref="infiniteScroll">
      <div class="image-container">
        <TagCard
          v-for="tag in imageCollection"
          :key="`tag-card-${tag.id}`"
          :item="tag"
          @click="onTagClick(tag.keyword)"
        ></TagCard>
      </div>
      <div class="text-container gap-row-8 gap-col-8">
        <div class="tag" v-for="tag in textCollection" :key="`tag-${tag.id}`" @click="onTagClick(tag.keyword)">
          <span>#{{ tag.keyword }}</span>
        </div>
      </div>
    </div>
    <Loading v-if="loading"></Loading>
    <div class="text-center font-size-14 font-weight-500 line-height-125" v-if="noMore">
      {{ $t('common.noMore') }}
    </div>
  </div>
</template>
<script setup>
import { computed, onMounted, ref, watch } from 'vue'
import { useInfiniteScroll } from '@vueuse/core'
import { storeToRefs } from 'pinia'
import { useSearchResultStore, useSearchStore } from '@/store/search'
import TagCard from '@/components/cards/TagCard.vue'
import Loading from '@/components/common/Loading.vue'
import { useInfinite } from '@/compositions/request/infinite'
import API from '@/http'

const searchStore = useSearchStore()
const { toTagListViewer } = searchStore
const { searchText, hasSearchText } = storeToRefs(useSearchResultStore())

const { dataList, dataExtra, loading, noMore, init, refresh, nextPage } = useInfinite({
  request: API.Search.tags,
  params: { keyword: searchText.value },
  limit: 50,
})
onMounted(init)

watch(searchText, (keyword) => {
  if (!hasSearchText.value) {
    return
  }
  refresh({ newParams: { keyword } })
})

const imageCollection = computed(() => dataList.value?.filter((item) => !!item.image_url))
const textCollection = computed(() => dataList.value?.filter((item) => !item.image_url))

const infiniteScroll = ref(null)
useInfiniteScroll(infiniteScroll, nextPage, { distance: 10, canLoadMore: () => !noMore.value })

function onTagClick(keyword) {
  toTagListViewer(keyword)
}
</script>
<style scoped lang="scss">
.root {
  padding-bottom: 4.625rem;
}
.image-container {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
}
.text-container {
  display: flex;
  flex-wrap: wrap;

  .tag {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    padding: 0.375rem 1rem;
    justify-content: center;
    align-items: center;

    border-radius: 3.125rem;
    background: var(--bg-dialog, #333546);
    border: 2px solid var(--color-primary-red, #333546);

    cursor: pointer;

    span {
      font-size: 0.875rem;
      font-weight: 500;
      line-height: 1.25rem;
    }
  }
}
</style>
